package servletAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import domain.dao.interfaces.IDAOContact;
import domain.dao.interfaces.IDAOContactGroup;
import actionForm.ActionDispatcherValidationForm;

public class ActionDispatcherAction extends Action {
	
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse) {
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();
		final ActionDispatcherValidationForm lForm = (ActionDispatcherValidationForm) pForm;
		
		final String selected_contact = lForm.getSelected_contact();
		final String wanted_action = lForm.getWanted_action();
		final String[] contactgroups = lForm.getContactgroups();
		int idContact = -1;

		

		try {
			pRequest.setAttribute("selected_contact", selected_contact);
			idContact = Integer.parseInt(selected_contact);
			if (wanted_action.compareTo("show_details_contact") == 0) {

				return pMapping.findForward("detail");

			} else if (wanted_action.compareTo("modify_contact") == 0) {
				
				return pMapping.findForward("modify");
				
			} else if (wanted_action.compareTo("delete_contact") == 0) {
				
				IDAOContact daoContact = (IDAOContact) context
						.getBean("beanDAOContact");

				daoContact.deleteContact(idContact);

				DomainUtils.displayInfoMessage(pRequest,
						DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR,
						"The contact has been successfully deleted.");

				return pMapping.findForward("stay");
				
			} else if (wanted_action.compareTo("add_to_contactgroup") == 0) {
				 IDAOContactGroup daoContactGroup = (IDAOContactGroup)
				 context.getBean("beanDAOContactGroup");
				 daoContactGroup.addContactToGroups(
				 contactgroups, idContact);

				DomainUtils
						.displayInfoMessage(pRequest,
								DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR,
								"The contact has been successfully added to the specified groups.");


				return pMapping.findForward("stay");
				
			} else if (wanted_action.compareTo("remove_from_contactgroup") == 0) {

				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context
						.getBean("beanDAOContactGroup");
				 daoContactGroup.removeContactFromGroups(
				 contactgroups, idContact);


				DomainUtils
						.displayInfoMessage(pRequest,
								DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR,
								"The contact has been successfully deleted from the specified groups.");


				return pMapping.findForward("stay");
				
			} else {
				throw new Exception();
			}
		} catch (Exception e) {

			if ((e instanceof FormArgumentsException) == false) {
				pRequest.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"An error occur on the server.");
			}

			DomainUtils.keepParametersContent(pRequest);
			
			return pMapping.findForward("stay");
		}
	}
}
